package com.shun.service.impl;

import com.shun.dao.UserDao;
import com.shun.pojo.User;
import com.shun.service.UserService;
import com.shun.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl implements UserService {

    public String login(String username, String password) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = mapper.selectOne(username);
        if(user!=null){
            if(username.equals(user.getUsername())&&password.equals(user.getPassword())){
                return username;
            }
            sqlSession.close();
        }
        return null;
    }

    public String register(String username, String password, String cPassword) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = mapper.selectOne(username);
        if(user!=null){
            return "该用户已存在";
        }
        if(username.equals("")||password.equals("")||cPassword.equals("")){
            return "请输入账号或密码！";
        }
        if(!password.equals(cPassword)){
            return "密码不一致";
        }
        if(password.equals(cPassword)){
            mapper.register(username,password);
            sqlSession.close();
        }
        return "注册成功";
    }
}
